import bpy
import bmesh

import 全局控制.Node.节点和插座基类.bl_节点基类 as 基类
import 节点.Core.bl_Mesh as 网格操作

import engine.initLIB.load_dll as engineLib
import ctypes




class C_Node_平滑网格法线(基类.C_Node_基类):
	bl_dname = 'bl_node_平滑网格法线'
	bl_label = '平滑网格法线'

	m_对象 = bpy.props.PointerProperty(type=bpy.types.Object)
	def init(self, context):
		super(C_Node_平滑网格法线, self).f_init(context)
		self.inputs.new('C_Socket_物体_Array1D', name='物体')
		#网格操作.f_创建网格插座(self, True)
		#self.outputs.new('C_Socket_物体', name='网格')
		self.f_toCPP()
		
	def draw_buttons(self, context, layout):
		layout.prop(self, "m_对象")
		
	def f_toCPP(self):
		engineLib.flexCAPI.f_bmesh平滑法线节点(ctypes.c_wchar_p(self.id_data.name), ctypes.c_wchar_p(self.name));
		#f_update_载入网格参数(self, None)
	def f_update(self):
		if self.m_对象 :
			顶点 = self.inputs['顶点'].f_getData()

			me = bpy.data.meshs.new('temp_mesh')
			bm = bmesh.new()
			bm.from_mesh(me)
			
			for v in 顶点:
				bm.verts.new(v)
			bm.to_mesh(self.m_对象.data)
			bm.free()

			bpy.ops.object.shade_smooth()
			#self.m_对象.data.from_pydata(vertices=顶点, edges=[], faces=[[0]])
			#tuple(chain.from_iterable(vertices))
			#print('顶点 = ', type(顶点[0]))
			#self.m_对象.data.vertices.foreach_set("co", tuple(chain.from_iterable(顶点)))
		pass


class C_NODE_MENU_平滑网格法线(bpy.types.Operator):
	bl_idname = 'node.global_node_som_mesh_normal'
	bl_label = '设置物体网格'
	bl_options = {'REGISTER', 'UNDO'}
	def execute(operator, context):
		bpy.ops.node.add_node(type='C_Node_平滑网格法线', use_transform=True)
		return {'FINISHED'}













classes = (
	C_Node_设置物体网格,
	C_NODE_MENU_设置物体网格,

)

def register():
	for c in classes:
		bpy.utils.register_class(c)
		
def unregister():
	for c in classes:
		bpy.utils.unregister_class(c)







